﻿using System.Collections.Generic;
using CTEM.Models;
using CTEM.Controllers.DAO;
using System;
using CTEM.Controllers.Global;

namespace CTEM.Controllers.BUS
{
    public class CommonBUS
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly CT_MaterialDAO _materialDAO = new CT_MaterialDAO();

        /// <summary>
        /// 
        /// </summary>
        private readonly CT_GroupMaterialDAO _groupMaterialDAO = new CT_GroupMaterialDAO();

        /// <summary>
        /// 
        /// </summary>
        private readonly CT_PurchaseBookDAO _purchaseBookDAO = new CT_PurchaseBookDAO();

        #region Business Login for Material Entity
        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialDTO"></param>
        /// <param name="errorMessage"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool ValidateMaterial(CT_MaterialDTO materialDTO, out string errorMessage, out string fieldName)
        {
            materialDTO.MaterialName = Global.Global.TitleCase(materialDTO.MaterialName);
            materialDTO.MaterialName = Global.Global.RemoveRedundantSpace(materialDTO.MaterialName);
            List<CT_MaterialDTO> listmaterial = LoadMaterial(true);
            if (materialDTO.MaterialName.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Điền Tên Vật Tư";
                fieldName = "MaterialName";
                return false;
            }
            else if (!materialDTO.MaterialName.Equals(Constants.EMPTY_TEXT))
            {
                foreach (var item in listmaterial)
                {
                    if (item.MaterialName.ToLower().Equals(materialDTO.MaterialName.ToLower()) && item.MaterialID != materialDTO.MaterialID)
                    {
                        errorMessage = "Tên Vật Tư Bạn Vừa Nhập Đã Tồn Tại";
                        fieldName = "MaterialName";
                        return false;
                    }

                }
                errorMessage = "";
                fieldName = "";
                return true;
            }
            else if (materialDTO.Unit.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Đơn Vị";
                fieldName = "Unit";
                return false;
            }
            else
            {
                errorMessage = "";
                fieldName = "";
                return true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialDTO"></param>
        /// <returns></returns>
        public long CreateMaterial(CT_MaterialDTO materialDTO)
        {
            materialDTO.MaterialName = Global.Global.TitleCase(materialDTO.MaterialName);
            materialDTO.MaterialName = Global.Global.RemoveRedundantSpace(materialDTO.MaterialName);
            materialDTO.Unit = Global.Global.TitleCase(materialDTO.Unit);
            materialDTO.Unit = Global.Global.RemoveRedundantSpace(materialDTO.Unit);
            return _materialDAO.CreateMaterial(materialDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialDTO"></param>
        /// <returns></returns>
        public bool UpdateMaterial(CT_MaterialDTO materialDTO)
        {
            materialDTO.MaterialName = Global.Global.TitleCase(materialDTO.MaterialName);
            materialDTO.MaterialName = Global.Global.RemoveRedundantSpace(materialDTO.MaterialName);
            materialDTO.Unit = Global.Global.TitleCase(materialDTO.Unit);
            materialDTO.Unit = Global.Global.RemoveRedundantSpace(materialDTO.Unit);
            materialDTO.UpdatedBy = Global.Global.CurrentUser.Username;
            materialDTO.LastUpdated = DateTime.Today;
            return _materialDAO.UpdateMaterial(materialDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public bool DeleteMaterial(long materialID)
        {
            return _materialDAO.DeleteMaterial(materialID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public CT_MaterialDTO GetMaterialByID(long materialID)
        {
            return _materialDAO.GetMaterialByID(materialID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isactive"></param>
        /// <returns></returns>
        public List<CT_MaterialDTO> LoadMaterial(bool isactive)
        {
            return _materialDAO.GetAllMaterial(isactive);
        }
        #endregion

        #region Business Logic for Group Material Entity
        internal bool ValidateGroupMaterial(CT_GroupMaterialDTO groupMaterialDTO, out string errorMessage, out string fieldName)
        {
            groupMaterialDTO.GroupMaterialName = Global.Global.TitleCase(groupMaterialDTO.GroupMaterialName);
            groupMaterialDTO.GroupMaterialName = Global.Global.RemoveRedundantSpace(groupMaterialDTO.GroupMaterialName);
            List<CT_GroupMaterialDTO> listgroupmaterial = LoadGroupMaterial(true);
            if (groupMaterialDTO.GroupMaterialName.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Tên Nhóm Vật Tư";
                fieldName = "GroupMaterialName";
                return false;
            }
            else if (groupMaterialDTO.GroupMaterialName.Length > 50)
            {
                errorMessage = "Tên Nhóm Vật Tư Không Được Dài Quá 50 Ký Tự";
                fieldName = "GroupMaterialName";
                return false;
            }
            else if (!groupMaterialDTO.GroupMaterialName.Equals(Constants.EMPTY_TEXT))
            {
                foreach (var item in listgroupmaterial)
                {
                    if (item.GroupMaterialName.ToLower().Equals(groupMaterialDTO.GroupMaterialName.ToLower()) && item.GroupMaterialID != groupMaterialDTO.GroupMaterialID)
                    {
                        errorMessage = "Tên Nhóm Vật Tư Bạn Vừa Nhập Đã Tồn Tại";
                        fieldName = "MaterialName";
                        return false;
                    }
                }
                errorMessage = "";
                fieldName = "";
                return true;
            }
            else
            {
                errorMessage = Constants.EMPTY_TEXT;
                fieldName = Constants.EMPTY_TEXT;
                return true;
            }
        }

        internal long CreateGroupMaterial(CT_GroupMaterialDTO groupMaterialDTO)
        {
            groupMaterialDTO.GroupMaterialName = Global.Global.TitleCase(groupMaterialDTO.GroupMaterialName);
            groupMaterialDTO.GroupMaterialName = Global.Global.RemoveRedundantSpace(groupMaterialDTO.GroupMaterialName);
            return _groupMaterialDAO.CreateGroupMaterial(groupMaterialDTO);
        }

        internal bool UpdateGroupMaterial(CT_GroupMaterialDTO groupMaterialDTO)
        {
            groupMaterialDTO.UpdatedBy = Global.Global.CurrentUser.Username;
            groupMaterialDTO.LastUpdated = DateTime.Today;
            groupMaterialDTO.GroupMaterialName = Global.Global.TitleCase(groupMaterialDTO.GroupMaterialName);
            groupMaterialDTO.GroupMaterialName = Global.Global.RemoveRedundantSpace(groupMaterialDTO.GroupMaterialName);
            return _groupMaterialDAO.UpdateGroupMaterial(groupMaterialDTO);
        }

        internal CT_GroupMaterialDTO GetGroupMaterialByID(long groupmaterialID)
        {
            return _groupMaterialDAO.GetGroupMaterialByID(groupmaterialID);
        }

        internal List<CT_GroupMaterialDTO> LoadGroupMaterial(bool isactive)
        {
            return _groupMaterialDAO.GetAllGroupMaterial(isactive);
        }

        internal bool DeleteGroupMaterial(long groupmaterialID)
        {
            return _groupMaterialDAO.DeleteGroupMaterial(groupmaterialID);
        }
        #endregion


        #region Business Login for Purchase Book
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PurchaseBookDTO"></param>
        /// <param name="errorMessage"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool ValidatePurchaseBook(CT_PurchaseBookDTO PurchaseBookDTO, out string errorMessage, out string fieldName)
        {
            PurchaseBookDTO.Officer = Global.Global.TitleCase(PurchaseBookDTO.Officer);
            PurchaseBookDTO.Officer = Global.Global.RemoveRedundantSpace(PurchaseBookDTO.Officer);
            if (PurchaseBookDTO.DebtID == 0)
            {
                errorMessage = "Bạn Chưa Chọn Nhà Cung Cấp";
                fieldName = "DebtID";
                return false;
            }
            else if (PurchaseBookDTO.Officer.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Người Chiệu Trách Nhiệm";
                fieldName = "Officer";
                return false;
            }
            else
            {
                errorMessage = "";
                fieldName = "";
                return true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PurchaseBookDTO"></param>
        /// <returns></returns>
        public long CreatePurchaseBook(CT_PurchaseBookDTO PurchaseBookDTO)
        {
            PurchaseBookDTO.Officer = Global.Global.TitleCase(PurchaseBookDTO.Officer);
            PurchaseBookDTO.Officer = Global.Global.RemoveRedundantSpace(PurchaseBookDTO.Officer);
            PurchaseBookDTO.Unit = Global.Global.TitleCase(PurchaseBookDTO.Unit);
            PurchaseBookDTO.Unit = Global.Global.RemoveRedundantSpace(PurchaseBookDTO.Unit);
            return _purchaseBookDAO.CreatePurchaseBook(PurchaseBookDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PurchaseBookDTO"></param>
        /// <returns></returns>
        public bool UpdatePurchaseBook(CT_PurchaseBookDTO PurchaseBookDTO)
        {
            PurchaseBookDTO.Officer = Global.Global.TitleCase(PurchaseBookDTO.Officer);
            PurchaseBookDTO.Officer = Global.Global.RemoveRedundantSpace(PurchaseBookDTO.Officer);
            PurchaseBookDTO.Unit = Global.Global.TitleCase(PurchaseBookDTO.Unit);
            PurchaseBookDTO.Unit = Global.Global.RemoveRedundantSpace(PurchaseBookDTO.Unit);
            PurchaseBookDTO.UpdatedBy = Global.Global.CurrentUser.Username;
            PurchaseBookDTO.LastUpdated = DateTime.Today;
            return _purchaseBookDAO.UpdatePurchaseBook(PurchaseBookDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public bool DeletePurchaseBook(long PurchaseBookID)
        {
            return _purchaseBookDAO.DeletePurchaseBook(PurchaseBookID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public CT_PurchaseBookDTO GetPurchaseBookByID(long PurchaseBookID)
        {
            return _purchaseBookDAO.GetPurchaseBookByID(PurchaseBookID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isactive"></param>
        /// <returns></returns>
        public List<CT_PurchaseBookDTO> LoadAllPurchaseBook()
        {
            return _purchaseBookDAO.LoadAllPurchaseBooks();
        }
        #endregion
    }
}
